package com.lzm.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lzm.mapper.DrugMapper;
import com.lzm.pojo.Drug;
import com.lzm.pojo.DrugExample;
import com.lzm.pojo.Hosregister;
import com.lzm.vo.QueryVo;
import com.lzm.vo.QueryVo2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class drugService {

    @Resource
    private DrugMapper drugMapper;
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public PageInfo<Drug> queryByPage(Integer pageNum, Integer pageSize, QueryVo2 vo) {
        DrugExample example = new DrugExample();
        DrugExample.Criteria criteria = example.createCriteria();
        if(vo!=null){
            if (vo.getDrug_name()!= null && !"".equals(vo.getDrug_name())){
                criteria.andDrug_nameLike("%"+vo.getDrug_name().trim()+"%");
            }
            if(vo.getDrug_type()!=null && !"".equals(vo.getDrug_type())){
                criteria.andDrug_typeEqualTo(vo.getDrug_type());
            }
        }
        //分页
        PageHelper.startPage(pageNum, pageSize);
        List<Drug> drugs = drugMapper.selectByExample(example);
        for (Drug drug : drugs) {
            drug.setDrug_state(0);
        }
        return new PageInfo<>(drugs);
    }

    public int addDrug(Drug drug){
        return drugMapper.insertSelective(drug);
    }
    public Drug queryById(String drug_id){
        Drug drug = drugMapper.selectByPrimaryKey(drug_id);
        return drug;
    }

    public int updateDrug(Drug drug){
        return drugMapper.updateByPrimaryKeySelective(drug);
    }
}
